<template>
    <div>
        <el-row class="row-with-margin">
            <el-col :span="24">
                <div>
                    <el-button>导出</el-button>
                    <el-button>导入</el-button>
                </div>
            </el-col>
        </el-row>


        <el-row class="row-with-margin" style="margin-top: 0px;margin-bottom: 0px;">
            <el-col :span="24">
                <el-form :inline="true">
                    <el-form-item label="创建日期">
                        <el-date-picker type="daterange" range-separator="至" start-placeholder="开始日期"
                            end-placeholder="结束日期" v-model="param.betweenTime">
                        </el-date-picker>
                    </el-form-item>

                    <el-form-item label="地址：" label-width="100px">
                        <el-input placeholder="请输入关键词搜索" style="width: 200px; margin-left: 10px;"
                            v-model="param.customerAddress"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="queryCustomerAddress">查询</el-button>
                        <el-button style="margin-left: 10px;" @click="resetForm">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>

        <el-row style="margin-top: 0px;margin-bottom: 0px;">
            <el-col :span="24">
                <div>
                    <el-tabs v-model="activeTab" @tab-click="handleTabClick">
                        <el-tab-pane label="客户收货地址" name="first"></el-tab-pane>
                    </el-tabs>
                </div>
            </el-col>
        </el-row>


        <el-row>
            <el-col :span="24">
                <div>
                    <el-table :data="tableData" style="width: 100%; margin-top: 20px;">
                        <el-table-column prop="name" label="客户名称"></el-table-column>
                        <el-table-column prop="companyContact" label="联系人"> </el-table-column>
                        <el-table-column prop="contactInfo" label="联系电话"></el-table-column>
                        <el-table-column prop="customerAddress" label="详细地址"></el-table-column>
                        <el-table-column prop="remarks" label="备注"></el-table-column>
                        <el-table-column label="操作" width="180">
                            <template slot-scope="scope">
                                <el-button type="text" size="medium"
                                    @click="updateCustomerAddress(scope.row)">编辑</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-col>
        </el-row>


        <el-row>
            <el-col :span="24">
                <el-pagination background :page-sizes="[10, 20, 30, 50]" :page-size="pageSize" :current-page="pageNum"
                    :total="total" layout="total,sizes,prev, pager, next,jumper" @next-click="nextPage"
                    @prev-click="prevPage" @current-change="changePage" @size-change="sizeChange">
                </el-pagination>
            </el-col>
        </el-row>



        <!--        //编辑弹框开始----------------------------------------------------------------------------------------------------->
        <el-dialog title="编辑用户信息" :visible.sync="updateCustomerAddressVisible">
            <el-form>
                <el-form-item label="客户名称" label-width="80px">
                    <el-input v-model="updateCustomer.name"></el-input>
                </el-form-item>

                <el-form-item label="联系人" label-width="80px">
                    <el-input v-model="updateCustomer.companyContact"></el-input>
                </el-form-item>

                <el-form-item label="联系电话" label-width="80px">
                    <el-input v-model="updateCustomer.contactInfo"></el-input>
                </el-form-item>

                <el-form-item label="详细地址" label-width="80px">
                    <el-input v-model="updateCustomer.customerAddress"></el-input>
                </el-form-item>

                <el-form-item label="备注" label-width="80px">
                    <el-input v-model="updateCustomer.remarks"></el-input>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="updateCustomerAddressVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveUpdateCustomerAddress">确 定</el-button>
            </div>
        </el-dialog>
        <!--        //编辑弹框结束----------------------------------------------------------------------------------------------------->
    </div>
</template>

<script>
export default {
    data() {
        return {
            activeTab: 'first',
            pageSize: 10,
            pageNum: 1,
            total: 0,
            tableData: [],//表格对象
            param: {},
            updateCustomerAddressVisible:false,
            updateCustomer:{},
        }
    },

    methods: {
        nextPage(val) {//当前页
            //下一页

            if (this.activeTab == 'first') {
                this.pageNum = val
                // 客户订单标签页被点击
                this.queryCustomerAddress();
            }
        },
        prevPage(val) {//当前页
            //上一页

            if (this.activeTab == 'first') {
                this.pageNum = val
                // 客户订单标签页被点击
                this.queryCustomerAddress();
            }
        },
        changePage(val) {
            //跳转页

            if (this.activeTab == 'first') {
                this.pageNum = val
                // 客户订单标签页被点击
                this.queryCustomerAddress();
            }
        },
        sizeChange(val) {
            //改变每页条数

            if (this.activeTab == 'first') {
                this.pageNum = 1
                this.pageSize = val
                // 客户订单标签页被点击
                this.queryCustomerAddress();
            }
        },
        // ---------------------------------------------分页方法分割线------------------------------------------------------------------------
        handleTabClick(tab) {
            if (tab.name === 'first') {
                this.tab = 'first'
                // 客户收货地址标签页被点击
                this.queryCustomerAddress();
            } else if (tab.name === 'second') {
                // 提货地址标签页被点击
                this.queryPickupAddress();
            }
        },
        resetForm() { //重置按钮调用方法
            this.param = { betweenTime: [], customerAddress: '' }
            this.queryCustomerAddress()
        },
        queryCustomerAddress() {
            let pageParam = {};
            pageParam.pageNum = this.pageNum;
            pageParam.pageSize = this.pageSize;
            pageParam.param = this.param;
            this.$axios.post('api/address/queryCustomerAddress', pageParam)
                .then(resp => {
                    console.log(resp.data);
                    if (resp.data.code === 200) {
                        this.tableData = resp.data.data.list
                        this.total = resp.data.data.total
                    }
                })
                .catch(error => {
                    console.error('操作失败:', error);
                    this.$message.error('网络请求失败，请稍后再试');
                });
        },
        queryPickupAddress() {

        },
        updateCustomerAddress(val) {
            this.updateCustomer = JSON.parse(JSON.stringify(val));
            this.updateCustomerAddressVisible = true
        },
        saveUpdateCustomerAddress(){
            this.$axios.post('api/address/saveUpdateCustomerAddress', this.updateCustomer)
                .then(resp => {
                    console.log(resp.data);
                    if (resp.data.code === 200) {
                        this.updateCustomerAddressVisible = false
                        this.queryCustomerAddress()
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                    }
                })
                .catch(error => {
                    console.error('操作失败:', error);
                    this.$message.error('网络请求失败，请稍后再试');
                });
        }
    },
    created() {
        this.queryCustomerAddress()
    }
}
</script>

<style scoped>
.row-with-margin {
    margin-bottom: 20px;
    /* 根据需要调整间隙大小 */
}
</style>